package com.atguigu.gmall.product.api;

import com.atguigu.gmall.common.cache.GmallCache;
import com.atguigu.gmall.product.model.*;
import com.atguigu.gmall.product.service.BaseCategoryViewService;
import com.atguigu.gmall.product.service.ProductApiService;
import com.atguigu.gmall.product.service.SkuManageService;
import com.atguigu.gmall.product.service.SpuManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author XIN
 * 2023/4/24
 */
@RestController
@RequestMapping("/api/product")
public class ProductApiController {
	@Autowired
	private ProductApiService productApiService;
	@Autowired
	private BaseCategoryViewService baseCategoryViewService;
	@Autowired
	private SkuManageService skuManageService;
	@Autowired
	private SpuManagerService spuManagerService;
	
	/**
	 * 根据SkuID查询SKU商品信息包含图片列表
	 * @param skuId
	 * @return
	 */
	@GetMapping("/inner/getSkuInfo/{skuId}")
	public SkuInfo getSkuInfo(@PathVariable Long skuId) {
		SkuInfo skuInfo = productApiService.getSkuInfo(skuId);
		return skuInfo;
	}
	
	/**
	 * 根据商品SKU三级分类ID查询分类信息
	 * @param category3Id
	 * @return
	 */
	@GetMapping("/inner/getCategoryView/{category3Id}")
	@GmallCache(prefix = "categoryView")
	public BaseCategoryView getCategoryView(@PathVariable Long category3Id) {
		
		return baseCategoryViewService.getById(category3Id);
	}
	
	/**
	 * 根据SKUID查询商品最新价格
	 * @param skuId
	 * @return
	 */
	@GetMapping("/inner/getSkuPrice/{skuId}")
	public BigDecimal getSkuPrice(@PathVariable Long skuId) {
		return skuManageService.getSkuPrice(skuId);
	}
	/**
	 * 根据spuId 获取海报数据
	 * @param spuId
	 * @return
	 */
	@GetMapping("/inner/findSpuPosterBySpuId/{spuId}")
	@GmallCache(prefix = "spuPosterBySpuId")
	public List<SpuPoster> getSpuPosterBySpuId(@PathVariable Long spuId) {
		return spuManagerService.getSpuPosterBySpuId(spuId);
	}
	/**
	 * 根据SkuID查询当前商品包含平台属性以及属性值
	 * @param skuId
	 * @return
	 */
	@GetMapping("/inner/getAttrList/{skuId}")
	@GmallCache(prefix = "attrList")
	public List<BaseAttrInfo> getAttrList(@PathVariable Long skuId) {
		return skuManageService.getAttrList(skuId);
	}
	
	/**
	 * 根据spuId,skuId 获取销售属性数据
	 * @param skuId
	 * @param spuId
	 * @return
	 */
	@GetMapping("/inner/getSpuSaleAttrListCheckBySku/{skuId}/{spuId}")
	@GmallCache(prefix = "spuSaleAttr")
	public List<SpuSaleAttr> getSpuSaleAttr(@PathVariable Long skuId, @PathVariable Long spuId) {
		List<SpuSaleAttr> spuSaleAttrs = spuManagerService.getSpuSaleAttrListCheckBySku(skuId,spuId);
		return spuSaleAttrs;
	}
	/**
	 * 查询每个商品SKU 销售属性 跟 SKUID 对照关系
	 *
	 * @param spuId
	 * @return {"3732|3734":24,"3732|3735":25}
	 */
	@GetMapping("/inner/getSkuValueIdsMap/{spuId}")
	@GmallCache(prefix = "skuValueIdsMap")
	public Map getSkuValueIdsMap (@PathVariable Long spuId) {
		Map<String,String> map= spuManagerService.getSkuValueIdsMap(spuId);
		return map;
	}
}
